/**
 * 
 */
package sopia.sg;

import sopia.cmm.CommonFunct;
import sopia.cmm.Constant;
import sopia.conn.AccountAdapter;
import sopia.conn.CustomerSpinnerAdapter;
import sopia.conn.DBAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author tatsu@gmail.com
 *
 */
public class G003Activity extends Activity {
	private AccountAdapter accAdapter ;
	private Button addAccButton ;
	private EditText nameAccountEdit ;
	private EditText amountAccountEdit;
	private Spinner unit_spin;
	private String screenID ;
	public static String valueCurrencySelected;
	public static String nameAccountAdded;
	public static boolean flagAdded = false;
	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.g003_layout);
		//init spinner
		Spinner unitSpinner = (Spinner) findViewById(R.id.unit_spin);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(
		            this, R.array.unit, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    unitSpinner.setAdapter(adapter);
	    // screen ID call
		Intent receiveIntent = this.getIntent();
		this.screenID = receiveIntent.getStringExtra("ID");
		// add account addAccName_button 
		addAccButton = (Button) findViewById(R.id.addAccName_button);
		nameAccountEdit = (EditText) findViewById(R.id.nameAcc_edit);
		amountAccountEdit = (EditText) findViewById(R.id.amountAcc_edit);
		valueCurrencySelected = Constant.EMPTY_STRING;
		unit_spin = (Spinner) findViewById(R.id.unit_spin);
		unit_spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				valueCurrencySelected = parent.getItemAtPosition(position).toString();
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		addAccButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				insertAccount(nameAccountEdit.getText().toString()
						,amountAccountEdit.getText().toString()
						,valueCurrencySelected);
			}
		});		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	  //Handle the back button
	  if (keyCode == KeyEvent.KEYCODE_BACK) {
		  backToPreviousScreen();
	    return true;
	  }

	  return super.onKeyDown(keyCode, event);
	} 
	private void insertAccount(String nameAccount,String amountAccount, String currency){
		try {
			accAdapter = new AccountAdapter(this);	
			accAdapter.beginTransaction();
			if(CommonFunct.isNullOrEmpty(nameAccount)){
				nameAccount = null;
				Toast.makeText(this,"Please input [Account name]!" , Toast.LENGTH_LONG).show();
				nameAccountEdit.setFocusable(true);
				return;
			}
			if(CommonFunct.isNullOrEmpty(amountAccount)){
				Toast.makeText(this,"Please input value Amount" , Toast.LENGTH_LONG).show();
				amountAccountEdit.selectAll();
				amountAccountEdit.requestFocus();
				return;
			}else{
				double amount = Double.parseDouble(amountAccount);
				if(0 == amount){
					Toast.makeText(this,"Please input value Amount more than zero" , Toast.LENGTH_LONG).show();
					amountAccountEdit.selectAll();
					amountAccountEdit.requestFocus();
					return;
				}
			}
			Long id = accAdapter.insertAccount(nameAccount.trim(), amountAccount, currency);
		
			//the row ID of the newly inserted row, or -1 if an error occurred 
			String contentMessage = "Account ["+nameAccount;
			if(id < 0){			
				contentMessage += "] really exist your database.";
				Toast.makeText(this,contentMessage , Toast.LENGTH_LONG).show();
				nameAccountEdit.selectAll();
				return;
			}else{
				contentMessage += "] was added.";				
				backToPreviousScreen();
				Toast.makeText(this, contentMessage , Toast.LENGTH_LONG).show();
			}
			accAdapter.setTransactionSuccessful();
			nameAccountAdded = nameAccount;
			flagAdded = true;
		} catch (Exception e) {
			Log.e("insertAccount", e.toString());
		}finally {
			accAdapter.endTransaction();
			accAdapter.close();
		}
	}
	private void backToPreviousScreen(){
		try {
			Intent i = null ;
			if("G002".equals(this.screenID)) {
				i = new Intent(G003Activity.this, G002Activity.class);
			} else if("G005".equals(this.screenID)) {
				i = new Intent(G003Activity.this, G005Activity.class);
			}
			i.putExtra("ID", "G003");
			startActivity(i);
			finish();
		} catch (Exception e) {
			Log.e("backToPreviousScreen", e.toString());
		}
	}
}
